1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use super::*;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Param {
pub name: Option<String>,
pub ty: Option<Arc<Expr>>,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Parametrized {
pub param: Param,
pub dep: Dependency,
pub value: Arc<Expr>,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum FnKind {
FnCopy,
Fn,
FnOnce,
ExCopy,
Fx,
Ex,
ExOnce,
FxOnce,
}
impl FnKind {
#[inline]
pub fn fun_lin(self, dep: Dependency) -> FunctionalLinearity {
let (linearity, call_usage) = self.linearity_call();
FunctionalLinearity::new(dep, linearity, call_usage)
}
#[inline]
pub fn linearity_call(self) -> (Linearity, Usage) {
use FnKind::*;
match self {
FnCopy => (Linearity::NONLINEAR, Usage::OBSERVED),
Fn => (Linearity::AFFINE, Usage::OBSERVED),
FnOnce => (Linearity::AFFINE, Usage::CONSUMED),
ExCopy => (Linearity::RELEVANT, Usage::UNUSED),
Fx => (Linearity::RELEVANT, Usage::OBSERVED),
Ex => (Linearity::LINEAR, Usage::UNUSED),
ExOnce => (Linearity::LINEAR, Usage::CONSUMED),
FxOnce => (Linearity::LINEAR, Usage::USED),
}
}
#[inline]
pub fn linearity(self) -> Linearity {
self.linearity_call().0
}
#[inline]
pub fn call_usage(self) -> Usage {
self.linearity_call().1
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Lambda(pub Option<FnKind>, pub Parametrized);
impl Lambda {
pub fn max_fun_lin(&self) -> FunctionalLinearity {
self.0
.map(|kind| kind.fun_lin(self.1.dep))
.unwrap_or_else(|| FunctionalLinearity::new(self.1.dep, Linearity::LINEAR, Usage::USED))
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Pi(pub FnKind, pub Parametrized);
impl Pi {
pub fn fun_lin(&self) -> FunctionalLinearity {
self.0.fun_lin(self.1.dep)
}
}